Repository navigation
[utest][atomic] release the worker threads and the semaphore between runs - #12033
Conversation
…runs
test_atomic_add() creates one semaphore and three dynamic threads per run and
released neither. Two separate leaks came out of that:
1. The three workers exit right after releasing the semaphore, so their TCB and
stack are only reclaimed once rt_defunct_execute() runs. That runs in the idle
thread, and the idle thread never gets scheduled while this testcase keeps a
runnable thread around. Running the testcase repeatedly in one boot therefore
accumulates dead threads.
2. The semaphore was never deleted.
Measured on bsp/qemu-vexpress-a9 with RT_UTEST_AUTO_RUN_LOOP=3, sampling the
thread list and the heap after every loop.
core.atomic alone:
before used 63608 -> 88848 -> 114088 (+25240 per run)
ps shows 3 -> 6 -> 9 lingering t1/t2/t3 threads, status "close"
after used 38368 -> 38368 -> 38368
ps shows 0 lingering threads each loop
{semaphore, event, atomic} together, where the idle thread does get a chance to
run between testcases and only the semaphore leak was visible:
before used 38440 -> 38504 -> 38568 (+64 per run, sizeof(rt_semaphore))
after used 38376 -> 38376 -> 38376
The fix is a yield before returning, so the defunct queue is processed, plus
rt_sem_delete(). The yield follows the same convention as hooklist_tc.c, which
waits for rt_defunct_execute() to detach the thread object.
Refs RT-Thread#11963
Refs RT-Thread#11983
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: kernelReviewers: @GorrayLi @ReviewSun @hamburger-os @lianux-mm @wdfk-prog @xu18838022837 Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-11-12 16:42 CST)
📝 Review Instructions
|
|
The comment claimed the yield "waits for the idle thread". rt_thread_mdelay() does not wait for anything specific: _thread_sleep() suspends the calling thread and reschedules, so the CPU goes to whatever else is ready. Reaching the idle thread is a consequence of giving up the CPU, not something the call targets. That distinction matters when reviewing the fix: it does not depend on idle being scheduled promptly, only on this thread stepping out of the ready queue, which _thread_sleep() does unconditionally. Also record both measured deltas so the constant is checkable: UTEST_THR_STACK_SIZE = 4096 gives 12952 bytes per run, 8192 gives 25240, and both reduce to the same rt_thread size of about 221 bytes.
|
补一组十轮数据和一条对修法的自我质疑,另外更正了注释里一句不准确的话(提交 十轮:严格线性,零抖动
九次增量完全相等,没有任何波动。这排除了"首次初始化固定开销"的可能——那就是每轮泄漏。
和 PR 描述里 25240 的差异:配置不同,不是测量误差PR 描述里写的是 +25240/轮,这里量到 +12952/轮。原因只有一个: 两组数据反算出的 TCB 大小都是 221.3 字节,完全一致。所以泄漏模型 这一点值得记录:这条泄漏的绝对量取决于配置,但"每轮 3 个线程"这个事实与配置无关。评审时如果只对比绝对数字会觉得对不上,对比"线程数增量"就一目了然。 自我质疑:
|
BernardXiong
left a comment
There was a problem hiding this comment.
批准。 复核 head 936b7413:断言后先 rt_thread_mdelay(1) 让 idle 线程回收 defunct 队列中的 3 个 worker,再删除信号量,修复了每轮 3 个线程加 1 个信号量的泄漏;做法与 hooklist_tc.c 的既有约定一致。CI 全绿(108 成功)。
Approved. The test now yields once so the idle thread can reap the three exited workers before deleting the semaphore, matching the existing convention in hooklist_tc.c. CI is green.
拉取/合并请求描述:(PR description)
[
为什么提交这份PR (why to submit this PR)
用 #11983 的多轮自动运行功能跑 utest 时发现
core.atomic每轮泄漏,而且有两种表现。test_atomic_add()每轮创建 1 个信号量和 3 个动态线程,一个都没有释放:两种泄漏:
rt_defunct_execute()才释放,而它跑在 idle 线程里。这个用例的 worker 是纯忙循环、utest 线程只阻塞在rt_sem_take上,idle(优先级 255)全程没有机会被调度,于是每轮堆下 3 个已退出的线程。你的解决方案是什么 (what is your solution)
在返回前让出一次 CPU(让 defunct 队列被处理),再删除信号量:
rt_thread_mdelay(1)这个约定在src/utest/hooklist_tc.c:88-91已有先例(那里注释写明是等rt_defunct_execute()摘除线程对象)。请提供验证的bsp和config (provide the config and bsp)
bsp/qemu-vexpress-a9RT_USING_UTESTCASES=y+RT_UTEST_USING_AUTO_RUN=y+RT_UTEST_AUTO_RUN_LOOP=35caa519be用例单独跑:线程累积
与其他用例一起跑:只剩信号量泄漏
{core.semaphore, core.ipc_event, core.atomic}一起跑时,中间有其他用例让 idle 拿到 CPU,线程能被回收,但信号量仍在漏:64 字节正好接近
sizeof(struct rt_semaphore)。这说明多轮运行时会看到两种量级完全不同的泄漏:只用例单跑,泄漏被线程累积掩盖成 25 KB/轮;与别的用例混跑,则表现为稳定的 64 字节/轮。任何一条单独看都可能被当成噪声忽略。
关于 #11983
这两处能发现,直接得益于 #11983 新加的
RT_UTEST_AUTO_RUN_LOOP与每轮free/ps采样。顺带确认一件事:采样动作本身不泄漏——我单独把msh_exec("free")+msh_exec("ps")连跑三轮,used稳定在 39824 不变,所以上面的增长不是观测行为引入的。]
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up